package com.java46.front.base.interceptor;


import com.java46.front.base.action.BaseAction;
import com.java46.front.base.entity.GenericResult;
import com.java46.front.base.exception.BusinessException;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


/**
 * @ClassName: ExceptionInterceptor 
 * @Description: 异常信息拦截器
 * @author: zuxia
 * @date: 2018年7月4日 下午3:00:46
 */
public class ExceptionInterceptor extends AbstractInterceptor {

	/** @Fields serialVersionUID : 序列化ID  **/
	private static final long serialVersionUID = 1964650817853124828L;
	//日志文件
	private final static Logger LOGGER = LogManager.getLogger(ExceptionInterceptor.class);
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		try {
			//数据正常返回result
			String result=invocation.invoke();
			return result;
		} catch (Exception e) {
			//异常数据统一放回"input"处理
			String errorMessage;
			if(e instanceof BusinessException) {
				BusinessException be=(BusinessException) e;
			    errorMessage=be.getErrorMessage();
			}else {
				errorMessage="系统出现异常信息，请处理";
			}
			LOGGER.error(e.getMessage());
			e.printStackTrace();
			//ajax请求
			//ajax异常信息
			//调用封装json的实体
			GenericResult result = new GenericResult();
			result.setSuccess(false);
			result.setErrorMessage(errorMessage);
			//调用Action的父类拿到action中的异常信息
			BaseAction baseAction = (BaseAction) invocation.getAction();
			baseAction.setResult(result);
			return "json";

		}
	}

}
